package com.oa.web.action;

import com.oa.bean.Customer;
import com.oa.bean.Emp;
import com.oa.service.CustomerService;
import com.oa.utils.PageBean;
import com.oa.web.action.base.BaseAction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Scope("prototype")
public class CustomerAction extends BaseAction<Customer> {

    @Autowired
    private CustomerService customerService;

    public String pageQuery() {

        DetachedCriteria dc = this.pageBean.getCriteria();

        Emp emp = model.getEmp();

        if (emp != null) {
            Integer id = emp.getId();
            if (id != null) {
                dc.add(Restrictions.eq("emp.id", id));
            }
        }

        Integer is = model.getIs();

        if (is != null) {
            dc.add(Restrictions.eq("is", is));
        }

        PageBean pb = customerService.pageQuery(this.pageBean);

        this.writeObj2Json(pb, new String[]{"currentPage", "pageSize", "criteria", "emp"});

        return NONE;
    }

    //添加
    public String add(){

        customerService.add(model);

        return NONE;
    }

    //删除客户，假删除
    public String delete(){

        customerService.delete(model.getId());
        this.writeText("1");
        return NONE;
    }

    //修改客户之前
    public String beforeEdit(){
        Customer cus = customerService.beforeEdit(model.getId());
        session.put("cus",cus);
        return "update";
    }

    //修改客户
    public String update(){

        customerService.update(model);

        return NONE;
    }

}
